﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

using Perseus;

namespace Hetymine.Executioner.Commands {
    public class CommandInfo {
        private string _Command;
        private string _Arguments;

        public CommandInfo() : this(string.Empty, string.Empty, false) { }
        public CommandInfo(string command) : this(command, string.Empty, false) { }
        public CommandInfo(string command, string arguments) : this(command, arguments, false) { }
        public CommandInfo(string command, string arguments, bool keepHistory) {
            this.Command = command;
            this.Arguments = arguments;
            this.KeepHistory = keepHistory;
        }
        public CommandInfo(XElement element) {
            if (element == null) {
                this.Command = string.Empty;
                this.Arguments = string.Empty;
            }
            else {
                this.Command = element.Value;
                if (element.Attribute("Args") != null) {
                    this.Arguments = element.Attribute("Args").Value;
                }
                else {
                    this.Arguments = string.Empty;
                }
            }
            this.KeepHistory = false;
        }
        
        public string Command {
            get { return this._Command; }
            set { this._Command = value.Trim(); }
        }
        public string Arguments {
            get { return this._Arguments; }
            set { this._Arguments = value.Trim(); }
        }

        public bool KeepHistory { get; set; }

        public override string ToString() {
            string command = this.Command;

            if (command.Is("@execute")) {
                command += ":";
            }
            else if (!command.StartsWith("@execute:", StringComparison.Ordinal)) {
                command = "\"" + command + "\"";
            }

            if (this.Arguments.IsNotEmpty()) {
                command += " " + this.Arguments;
            }

            return command;
        }

        public bool IsEmpty {
            get {
                return (this.Command.IsEmpty());
            }
        }
    }
}
